package com.github.excel.remote;

import cn.hutool.core.exceptions.ValidateException;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.github.excel.entity.vo.response.ResponseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class OtherRequest {

    private final Logger logger = LoggerFactory.getLogger(OtherRequest.class);

    @Value("${mall.cus_url}")
    private String CUSUrl;

    /**
     * 根据文件id获取文件base64编码
     */
    public String getFileBase64(String fileId){
        if (StrUtil.isBlank(fileId)) {
            throw new ValidateException("文件id不能为空");
        }else {
            //调用cus接口
            try {
                String theURL = CUSUrl + "/utils/downFileByte";
                String result = HttpRequest.post(theURL).form("fileId", fileId).execute().body();
                ResponseData<String> responseData = JSON.parseObject(result, ResponseData.class);
                if (responseData == null) {
                    throw new ValidateException("获取文件失败");
                }
                if (0 != responseData.getCode()) {
                    throw new ValidateException(responseData.getMessage());
                }
                return responseData.getData();
            } catch (Exception e) {
                logger.info(e.getMessage());
                throw new ValidateException("获取文件失败");
            }
        }
    }
}
